var houseFloorPlanName;

$(document).ready(function () {
    layui.use(['form', 'upload'], function () {
        form = layui.form;
        upload = layui.upload;

        form.render();
        initUpload();
        getFloorPlan();
    });
});

function initUpload() {

    upload.render({
        elem: '#floor-plan-btn-choose'
        , auto: false
        , accept: 'image'
        , exts: 'jpg|png|gif'
        , headers: {'Authorization': token}
        , method: 'POST'
        , url: apiHostUrl + '/admin/floorplan/editFloorPlan'
        , bindAction: '#floor-plan-btn-submit'
        , field: "houseFloorPlanFile"
        , data: {
            houseFloorPlanName: function () {
                return $("#floorPlanName").val();
            },
            houseFloorPlanId: function () {
                return $("#floorPlanId").val();
            }
        }
        , choose: function (obj) {
            // 预读本地文件
            obj.preview(function (index, file, result) {
                $('#img-header').attr('src', result);
            });
        }
        , done: function (result, index, upload) {
            if (result.errorCode === "0") {
                layer.msg("操作成功！");
                loadingPage("/admin/floorplan/floorplan");
            }
        }
        , error: function (index, upload) {
            // layer.msg("上传失败");
        }
    });
}

function getFloorPlan() {

    var id = $('#id').val();

    ajax(
        "/admin/floorplan/getFloorPlan", "POST",
        { houseFloorPlanId: id }
        ,true, true, false
    ).done(function (returnData) {

        $("input[name='floorPlanId']").val(returnData.houseFloorPlanId);
        $("input[name='houseFloorPlanName']").val(returnData.houseFloorPlanName);
        $("#img-header").attr("src", "data:image/jpg;base64," + returnData.houseFloorPlanFileBase64);

    });

}
